import { Controller, Get, Post, Body, Query, Inject } from '@nestjs/common';
import { UserService } from './user.service';
import { RegisterUserDto } from './dto/register-user.dto';
import { LoginUserDto } from './dto/login-user.dto';
import { RequireLogin, UserInfo } from 'src/custom.decorator';
import { UpdateUserPasswordDto } from './dto/updateUserPasswordDto';
import { UpdateUserDto } from './dto/update-user.dto';

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  // 注册邮箱验证码接口
  @Get('register_captcha')
  async captcha(@Query('email') email: string) {
    return this.userService.sendCaptcha(email);
  }

  // 更新密码邮箱验证码接口
  @Get('update_password_captcha')
  async updatePasswordCaptcha(@Query('email') email: string) {
    return this.userService.sendUpdatePasswordCaptcha(email);
  }

  // 用户注册
  @Post('register')
  async register(@Body() registerUser: RegisterUserDto) {
    return this.userService.register(registerUser);
  }

  // 用户登录
  @Post('login')
  async login(@Body() loginUser: LoginUserDto) {
    return await this.userService.login(loginUser);
  }

  // 更新用户信息
  @Post('update')
  @RequireLogin()
  async update(
    @UserInfo('userId') userId: number,
    @Body() updateUserDto: UpdateUserDto,
  ) {
    return await this.userService.update(userId, updateUserDto);
  }

  // 更新用户密码
  @Post('update_password')
  @RequireLogin()
  async updatePassword(@Body() passwordDto: UpdateUserPasswordDto) {
    return await this.userService.updateUserPassword(passwordDto);
  }

  // 获取用户信息
  @Get('info')
  @RequireLogin()
  async info(@UserInfo('userId') userId: number) {
    return this.userService.getUserInfoById(userId);
  }

  @Post('logout')
  async logout(@UserInfo('userId') userId: number) {
    return this.userService.logout();
  }
}
